import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
import {List, Detail, Brand} from './DTO';
const baseUrl = '/sales/api/v1';
const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '整车包装箱单'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const closeDesc = formatMessage({
    id: 'api.message.closeDesc',
    defaultMessage: '关闭{resourceName}'
}, {resourceName});

const brandDesc = formatMessage({
    id: 'api.message.brandDesc',
    defaultMessage: '查询品牌'
});

// 查询
export const getList = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/salePackingLists?${queryString}`, null, List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id => request(`${baseUrl}/salePackingLists/${id}`, null, Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery, getDetailDesc));

// 编辑
export const updateInfo = (id, data) => request(`${baseUrl}/salePackingLists/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 关闭
export const closeInfo = (id, data) => request(`${baseUrl}/salePackingLists/${id}/close`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, closeDesc));


import {baseDataStatus} from '../Enum';
// 获取品牌
export const getBrands = () => {
    const queryCondition = stringify({
        status: baseDataStatus.生效
    });
    return request(`${baseUrl}/brands?${queryCondition}`, null, Brand)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, brandDesc));
};

//导入模板
export const importTemp = () => request(`${baseUrl}/salePackingLists/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
//KD索赔箱单导入模板
export const KdImportTemp = () => request(`${baseUrl}/salePackingLists/KdImportTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, null)).then(res => res.data);
